/*
 * @Description: 
 * @Author: duan_shuangjun 丶
 * @Date: 2025-08-20 20:39:28
 * @LastEditTime: 2025-08-20 20:39:57
 */
type Handler<T = any> = (payload?: T) => void

class SimpleBus {
    private events: Map<string, Set<Handler>> = new Map()

    on<T = any>(event: string, handler: Handler<T>) {
        if (!this.events.has(event)) this.events.set(event, new Set())
        this.events.get(event)!.add(handler as Handler)
    }

    off<T = any>(event: string, handler: Handler<T>) {
        this.events.get(event)?.delete(handler as Handler)
    }

    emit<T = any>(event: string, payload?: T) {
        this.events.get(event)?.forEach(fn => fn(payload))
    }

    clear() {
        this.events.clear()
    }
}

export const bus = new SimpleBus()
export default bus
